using System;
using System.Collections;
using UnityEngine;

public class QualityController : MonoBehaviour
{
	public static QualityController instance;

	private static string keyScale = "Quality_Screen";

	private static string keyTexture = "Quality_Texture";

	private static string keyDetail = "Quality_Detail";

	private static string keyLight = "Quality_Light";

	private static string keySfx = "Quality_Sfx";

	private string keyQualityAlreadySet = "QualityAlreadySet";

	private int startSizeWidth;

	private int startSizeHeight;

	private static bool startParamSet;

	private static string keyFirstResWidth = "keyFirstResWidth";

	private static string keyFirstResHeight = "keyFirstResHeight";

	public int ScaleScreen
	{
		get
		{
			return Load.LoadInt(keyScale);
		}
		set
		{
			int num = value;
			if (num > 2)
			{
				num = 2;
			}
			if (num < 0)
			{
				num = 0;
			}
			if (!startParamSet || !num.Equals(ScaleScreen))
			{
				BCWDebug.Log(">>>>> ScaleScreen: " + num);
				Save.SaveInt(keyScale, num);
				float num2 = 0.15f;
				if (startSizeHeight > 800)
				{
					num2 = 0.23f;
				}
				BCWDebug.Log(">>>>> ScaleScreen, current res: " + Screen.width + ", " + Screen.height);
				switch (num)
				{
				case 0:
					Screen.SetResolution(Mathf.RoundToInt(startSizeWidth), Mathf.RoundToInt(startSizeHeight), true);
					BCWDebug.Log(">>>>> ScaleScreen, set res: " + Mathf.RoundToInt(startSizeWidth) + ", " + Mathf.RoundToInt(startSizeHeight));
					break;
				case 1:
					Screen.SetResolution(Mathf.RoundToInt((float)startSizeWidth * 0.85f), Mathf.RoundToInt((float)startSizeHeight * (1f - num2)), true);
					BCWDebug.Log(">>>>> ScaleScreen, set res: " + Mathf.RoundToInt((float)startSizeWidth * 0.85f) + ", " + Mathf.RoundToInt((float)startSizeHeight * (1f - num2)));
					break;
				case 2:
					Screen.SetResolution(Mathf.RoundToInt((float)startSizeWidth * 0.7f), Mathf.RoundToInt((float)startSizeHeight * (1f - num2 - num2)), true);
					BCWDebug.Log(">>>>> ScaleScreen, set res:  " + Mathf.RoundToInt((float)startSizeWidth * 0.7f) + ", " + Mathf.RoundToInt((float)startSizeHeight * (1f - num2 - num2)));
					break;
				}
				if (startParamSet && QualityController.onChangeQuality != null)
				{
					QualityController.onChangeQuality();
				}
			}
		}
	}

	public int QualityTexture
	{
		get
		{
			return Load.LoadInt(keyTexture);
		}
		set
		{
			int num = value;
			if (num > 2)
			{
				num = 2;
			}
			if (num < 0)
			{
				num = 0;
			}
			if (!startParamSet || !num.Equals(QualityTexture))
			{
				BCWDebug.Log(">>>>> QualityTexture: " + num);
				Save.SaveInt(keyTexture, num);
				switch (num)
				{
				case 0:
					QualitySettings.SetQualityLevel(2, true);
					break;
				case 1:
					QualitySettings.SetQualityLevel(1, true);
					break;
				case 2:
					QualitySettings.SetQualityLevel(0, true);
					break;
				}
				if (startParamSet && QualityController.onChangeQuality != null)
				{
					QualityController.onChangeQuality();
				}
			}
		}
	}

	public static bool DetailLevel
	{
		get
		{
			return Load.LoadBool(keyDetail);
		}
		set
		{
			if (!startParamSet || !value.Equals(DetailLevel))
			{
				Save.SaveBool(keyDetail, value);
				BCWDebug.Log(">>>>> DetailLevel: " + value);
				if (startParamSet && QualityController.onChangeQuality != null)
				{
					QualityController.onChangeQuality();
				}
			}
		}
	}

	public static bool LightEnable
	{
		get
		{
			return Load.LoadBool(keyLight);
		}
		set
		{
			if (!startParamSet || !value.Equals(LightEnable))
			{
				Save.SaveBool(keyLight, value);
				if (startParamSet && QualityController.onChangeQuality != null)
				{
					QualityController.onChangeQuality();
				}
			}
		}
	}

	public static bool SfxSoundAndParticle
	{
		get
		{
			return Load.LoadBool(keySfx);
		}
		set
		{
			if (!startParamSet || !value.Equals(SfxSoundAndParticle))
			{
				Save.SaveBool(keySfx, value);
				if (startParamSet && QualityController.onChangeQuality != null)
				{
					QualityController.onChangeQuality();
				}
			}
		}
	}

	public int TotalQuality
	{
		get
		{
			if (LightEnable && SfxSoundAndParticle && DetailLevel && QualityTexture == 0 && ScaleScreen == 0)
			{
				return 1;
			}
			if (LightEnable && !SfxSoundAndParticle && !DetailLevel && QualityTexture == 1 && ScaleScreen == 0)
			{
				return 2;
			}
			if (!LightEnable && !SfxSoundAndParticle && !DetailLevel && QualityTexture == 2 && ScaleScreen == 1)
			{
				return 3;
			}
			return 0;
		}
		set
		{
			BCWDebug.Log(">>>>> TotalQuality, set value: " + value);
			switch (value)
			{
			case 1:
				LightEnable = true;
				SfxSoundAndParticle = true;
				DetailLevel = true;
				QualityTexture = 0;
				ScaleScreen = 0;
				break;
			case 2:
				LightEnable = true;
				SfxSoundAndParticle = false;
				DetailLevel = false;
				QualityTexture = 1;
				ScaleScreen = 0;
				break;
			case 3:
				LightEnable = false;
				SfxSoundAndParticle = false;
				DetailLevel = false;
				QualityTexture = 2;
				ScaleScreen = 1;
				break;
			}
		}
	}

	public static event Action onChangeQuality;

	private void Awake()
	{
		instance = this;
		if (Load.LoadInt(keyFirstResWidth) > 0)
		{
			startSizeWidth = Load.LoadInt(keyFirstResWidth);
		}
		else
		{
			startSizeWidth = Screen.width;
			Save.SaveInt(keyFirstResWidth, startSizeWidth);
		}
		if (Load.LoadInt(keyFirstResHeight) > 0)
		{
			startSizeHeight = Load.LoadInt(keyFirstResHeight);
		}
		else
		{
			startSizeHeight = Screen.height;
			Save.SaveInt(keyFirstResHeight, startSizeHeight);
		}
		StartCoroutine(InitStart());
	}

	private void OnDestroy()
	{
		instance = null;
	}

	private IEnumerator InitStart()
	{
		startParamSet = false;
		if (!Load.LoadBool(keyQualityAlreadySet))
		{
			TotalQuality = Device.indexWeakDevice + 1;
			yield return new WaitForSeconds(0.2f);
			TotalQuality = Device.indexWeakDevice + 1;
			Save.SaveBool(keyQualityAlreadySet, true);
		}
		else
		{
			TotalQuality = TotalQuality;
		}
		startParamSet = true;
		PlayerPrefs.Save();
	}

	public void UpdateQuality()
	{
		ScaleScreen = ScaleScreen;
		QualityTexture = QualityTexture;
	}

	public static void InstIfNeed()
	{
		if (instance == null)
		{
			GameObject gameObject = new GameObject("QualityController");
			QualityController qualityController = gameObject.AddComponent<QualityController>();
			instance = qualityController;
		}
	}
}
